package com.qjs.activity.rookie.exception;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.method.HandlerMethod;
import org.springframework.web.servlet.HandlerExceptionResolver;
import org.springframework.web.servlet.ModelAndView;

import com.qjs.commom.ResponseUtils;

public class MyExceptionResolver implements HandlerExceptionResolver {
	public static final Logger log = LoggerFactory.getLogger(MyExceptionResolver.class);

	@Override
	public ModelAndView resolveException(HttpServletRequest request, HttpServletResponse response, Object handler,
			Exception ex) {
		log.debug("resolveException开始");
		ModelAndView vm = new ModelAndView();
		HandlerMethod handlerMethod = (HandlerMethod) handler;
		ResponseBody body = handlerMethod.getMethodAnnotation(ResponseBody.class);
		System.out.println("1" + ex.getMessage());
		System.out.println("2" + ex);
		if (body != null) {
			try {
				// 返回json数据
				ResponseUtils.renderJson(response, "系统异常");
				return null;
			} catch (Exception e1) {
				e1.printStackTrace();
			}
		}
		vm.addObject("message", ex.getMessage());
		vm.setViewName("500");
		return vm;

	}

}
